<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改食谱类别')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-category-edit" th:object="${recipeCategory}">
			<input id="categoryId" name="categoryId" type="hidden" th:field="*{categoryId}"/>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">类别名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="categoryName" id="categoryName" th:field="*{categoryName}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类别图片：</label>
				<div class="col-sm-8">
					<input class="form-control" type="hidden" name="categoryPic" id="pic" th:field="*{categoryPic}">
					<a id="url"><img style="width: 90px;height: 90px" th:src="*{categoryPic}"></a>
					<input type="file" id="categoryPic">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示顺序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="categorySort" id="categorySort" th:field="*{categorySort}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类别状态：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">小程序可见：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="visible" th:value="${dict.dictValue}" th:field="*{visible}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<textarea id="remark" name="remark" class="form-control">[[*{remark}]]</textarea>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "recipe/category";
		
		$("#form-category-edit").validate({
			onkeyup: false,
			rules:{
				categoryName:{
					remote: {
		                url: ctx + "recipe/category/checkCategoryNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"categoryId": function() {
							    return $("input[name='categoryId']").val();
							},
		                	"categoryName" : function() {
		                        return $.common.trim($("#categoryName").val());
		                    }
		                }
		            }
				},
				categorySort:{
					digits:true
				},
			},
			messages: {
		        "categoryName": {
		            remote: "类别名称已经存在"
		        }
		    },
		    focusCleanup: true
		});

		// 上传文件
		$("#categoryPic").change(function () {
			var data = new FormData();
			data.append("file", $("#categoryPic")[0].files[0]);
			$.ajax({
				type: "POST",
				url: ctx + "common/upload",
				data: data,
				cache: false,
				contentType: false,
				processData: false,
				dataType: 'json',
				success: function(result) {
					// alert(JSON.stringify(result))
					if (result.code == web_status.SUCCESS) {
						$("#url img").attr("src",result.fileName)
						$("#pic").val(result.fileName)
					}
				},
				error: function(error) {
					alert("图片上传失败。");
				}
			});
		})

		function submitHandler() {
	        if ($.validate.form()) {
	        	$.operate.save(prefix + "/edit", $('#form-category-edit').serialize());
	        }
	    }
	</script>
</body>
</html>
